package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.efounder.chat.db.WeChatDBManager;
import com.efounder.chat.utils.CommonThreadPoolUtils;
import com.efounder.chat.utils.LXGlideImageLoader;
import com.efounder.chat.widget.RoundImageView;
import com.efounder.chat.zxing.qrcode.QRCode;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.mobilecomps.contacts.User;
import com.efounder.util.AppContext;
import com.efounder.util.ToastUtil;
import com.efounder.utils.ImageSpliceUtil;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.qmuiteam.qmui.util.QMUIDrawableHelper;
import com.utilcode.util.ImageUtils;
import com.utilcode.util.ReflectUtils;

import java.io.File;

import static com.efounder.frame.utils.Constants.CHAT_USER_ID;

/**
 * 红包海报页面
 *
 * @author yqs
 */

public class LuckMoneyPosterActivity extends TCBaseActivity implements View.OnClickListener {


    private final String TAG = "LuckMoneyPosterActivity";
    private ImageView ivBack;
    private TextView tvTitle;
    private TextView tvTopTitle;
    private ImageView ivBg;
    private RoundImageView ivAvatar;
    private TextView tvName;
    private TextView tvContent1;
    private ImageView ivQrcode;
    private TextView tvBottomTips;
    private LinearLayout llSave;
    private LinearLayout llShare;
    private ScrollView scrollview;
    private LinearLayout llContent;

    private String redPackageUrl;
    private String unit;
    private String amount;

    private ReflectUtils reflectUtils;


    /**
     * 跳转页面
     *
     * @param context
     * @param redPackageUrl 红包地址
     * @param unit          单位 PWR
     * @param amount        数量
     */
    public static void start(Context context, String redPackageUrl, String unit, String amount) {
        Intent starter = new Intent(context, LuckMoneyPosterActivity.class);
        starter.putExtra("redPackageUrl", redPackageUrl);
        starter.putExtra("unit", unit);
        starter.putExtra("amount", amount);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.assets_activity_luck_money_poster);
        initView();
        initListener();
        initData();
    }


    private void initView() {
        ivBack = (ImageView) findViewById(R.id.iv_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTopTitle = (TextView) findViewById(R.id.tv_top_title);
        ivBg = (ImageView) findViewById(R.id.iv_bg);
        ivAvatar = (RoundImageView) findViewById(R.id.iv_avatar);
        tvName = (TextView) findViewById(R.id.tv_name);
        tvContent1 = (TextView) findViewById(R.id.tv_content1);
        ivQrcode = (ImageView) findViewById(R.id.iv_qrcode);
        tvBottomTips = (TextView) findViewById(R.id.tv_bottom_tips);
        llSave = (LinearLayout) findViewById(R.id.ll_save);
        llShare = (LinearLayout) findViewById(R.id.ll_share);
        llSave.setOnClickListener(this);
        llShare.setOnClickListener(this);
        scrollview = (ScrollView) findViewById(R.id.scrollview);
        llContent = (LinearLayout) findViewById(R.id.ll_content);

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            reflectUtils.method("release", this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initData() {
        redPackageUrl = getIntent().getStringExtra("redPackageUrl");
        unit = getIntent().getStringExtra("unit");
        amount = getIntent().getStringExtra("amount");
        User selefUser = WeChatDBManager.getInstance().getOneUserById(Integer.valueOf(EnvironmentVariable.getProperty(CHAT_USER_ID)));

        tvName.setText(selefUser.getNickName());
        tvTitle.setText(R.string.open_planet_packet_poster);
        tvContent1.setText(getResources().getString(R.string.open_planet_packet_poster_content1, amount, unit));
        LXGlideImageLoader.getInstance().showUserAvatar(this, ivAvatar, selefUser.getAvatar());
        Bitmap bm = QRCode.setQRcode(redPackageUrl, 800, 800);
        if (bm != null) {
            ivQrcode.setImageBitmap(bm);
        }
        //初始化友盟分享
        try {
            reflectUtils = ReflectUtils.reflect(ResStringUtil.getString(R.string.manager_share_screen_shot_class));
            reflectUtils = reflectUtils.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_save) {
            createAndSaveScreenShot(false);
        } else if (id == R.id.ll_share) {
            createAndSaveScreenShot(true);
        }
    }

    private void createAndSaveScreenShot(final boolean isShare) {
        showLoading(R.string.common_text_please_wait);

        CommonThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = QMUIDrawableHelper.createBitmapFromView(llContent);
                final String fielPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()
                        + File.separator
                        + System.currentTimeMillis() + ".png";
                ImageUtils.save(bitmap, fielPath, Bitmap.CompressFormat.PNG, true);
                //通知图库刷新
                ImageSpliceUtil.galleryAddPic(fielPath);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!LuckMoneyPosterActivity.this.isDestroyed() && !LuckMoneyPosterActivity.this.isFinishing()) {
                            dismissLoading();
                            if (isShare) {
                                try {
                                    reflectUtils.method("share", LuckMoneyPosterActivity.this, fielPath,
                                            fielPath, R.drawable.ic_launcher, "", null);
                                } catch (Exception e) {
//                                    LogUtils.d("没有找到分享的类，分享原图");
                                    new File(fielPath).delete();
                                }

                            } else {
                                ToastUtil.showToast(AppContext.getInstance(), R.string.open_planet_packet_poster_saveed);

                            }

                        }

                    }
                });
            }
        });
    }


    private void initListener() {

    }

}
